/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.io {
	import com.proyecto404.core.AssetInfoTypes;
	import com.proyecto404.core.BaseObject;
	import com.proyecto404.core.EventArgs;
	import com.proyecto404.core.JobQueue;
	import com.proyecto404.core.collections.IQueue;
	import com.proyecto404.core.collections.Queue;
	import com.proyecto404.core.collections.Set;
	
	import org.osflash.signals.ISignal;
	import org.osflash.signals.Signal;
	
	public class MultiAssetLoader extends BaseObject {
		private var _jobQueue:JobQueue;
		private var _totalFiles:int;
		private var _progress:Signal;
		
		public function MultiAssetLoader() {
			_jobQueue = new JobQueue();
			_totalFiles = 0;
			_progress = new Signal(MultiFileLoadProgressEventArgs);
		}
		
		public function get allLoaded():ISignal {
			return _jobQueue.allJobsExecuted;
		}
		
		public function get error():ISignal {
			return _jobQueue.error;
		}

		public function get progress():ISignal {
			return _progress;
		}

		public function addAsset(file:String,type:AssetInfoTypes, callback:Function):void {
			var loadJob:BaseObject;
			var fileNumber:int;
			switch(type)
			{
				case AssetInfoTypes.Swf:
				case AssetInfoTypes.Bitmap:					
					loadJob = new LoadDisplayObjectJob(file);
					_totalFiles++;
					fileNumber = _totalFiles;
					LoadDisplayObjectJob(loadJob).progress.add(function(e:FileLoadProgressEventArgs):void {
						_progress.dispatch(new MultiFileLoadProgressEventArgs(e.bytesLoaded, e.bytesTotal, fileNumber, _totalFiles));
					});
					_jobQueue.addJob(LoadDisplayObjectJob(loadJob), callback);
				break;
				
				case AssetInfoTypes.Sound:
					loadJob = new LoadSoundJob(file);
					
					_totalFiles++;
					fileNumber = _totalFiles;
					LoadSoundJob(loadJob).progress.add(function(e:FileLoadProgressEventArgs):void {
						_progress.dispatch(new MultiFileLoadProgressEventArgs(e.bytesLoaded, e.bytesTotal, fileNumber, _totalFiles));
					});
					_jobQueue.addJob(LoadSoundJob(loadJob), callback);
				break;
			}
		}
		
		public function loadAll():void {
			_jobQueue.execute();
		}
	}
}
